package com.lwl.blue.seven.controller;

import com.lwl.blue.commont.base.JsonResult;
import com.lwl.blue.commont.base.PageResult;
import com.lwl.blue.commont.base.PageParam;
import com.lwl.blue.seven.service.Contract7Service;
import com.lwl.blue.seven.dto.*;
import com.lwl.blue.seven.constants.ContractStatus;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import org.springframework.web.bind.annotation.CrossOrigin;

/**
 * 采购合同主表(Contract7)表控制层
 *
 * @author makejava
 * @since 2025-09-17 14:45:29
 */
@RestController
@RequestMapping("/contract7")
@Validated
@CrossOrigin(origins = "*") // 允许所有源
public class Contract7Controller {
    
    @Resource
    private Contract7Service contract7Service;

    /**
     * 分页查询合同列表（支持搜索和筛选）
     */
    @PostMapping("/list")
    public PageResult<Contract7ListVO> queryContractList(
            @RequestBody PageParam<Contract7QueryDTO> pageParam) {
        
        // 从session或token中获取当前用户ID，这里模拟
        Long currentUserId = 1l;

        return contract7Service.queryContractList(pageParam, currentUserId);
    }

    /**
     * 根据ID查询合同详情
     */
    @GetMapping("/{contractId}")
    public JsonResult<Contract7DetailDTO> getContractDetail(
            @PathVariable Long contractId) {
        
        Long currentUserId = getCurrentUserId();
        Contract7DetailDTO detail = contract7Service.getContractDetail(contractId, currentUserId);
        
        if (detail == null) {
            return new JsonResult().error("合同不存在");
        }
        
        return new  JsonResult().success(detail);
    }

    /**
     * 创建新合同
     */
    @PostMapping("/create")
    public JsonResult<Long> createContract(
            @Valid @RequestBody Contract7CreateDTO createDTO) {
        
        Long currentUserId = getCurrentUserId();
        return contract7Service.createContract(createDTO, currentUserId);
    }
    /**
     * 更新合同
     */
    @PutMapping("/{contractId}")
    public JsonResult<Void> updateContract(
            @PathVariable Long contractId,
            @Valid @RequestBody Contract7CreateDTO createDTO) {
        
        Long currentUserId = getCurrentUserId();
        return contract7Service.updateContract(contractId, createDTO, currentUserId);
    }

    /**
     * 删除合同（软删除）
     */
    @DeleteMapping("/{contractId}")
    public JsonResult<Void> deleteContract(
            @PathVariable Long contractId) {
        
        Long currentUserId = getCurrentUserId();
        return contract7Service.deleteContract(contractId, currentUserId);
    }

    /**
     * 批量删除合同
     */
    @DeleteMapping("/batch")
    public JsonResult<Void> batchDeleteContract(
            @RequestBody List<Long> contractIds) {
        
        Long currentUserId = getCurrentUserId();
        return contract7Service.batchDeleteContract(contractIds, currentUserId);
    }

    /**
     * 提交合同审批
     */
    @PostMapping("/{contractId}/submit")
    public JsonResult<Void> submitContract(
            @PathVariable Long contractId) {
        
        Long currentUserId = getCurrentUserId();
        return contract7Service.submitContract(contractId, currentUserId);
    }

    /**
     * 审核通过合同
     */
    @PostMapping("/{contractId}/approve")
    public JsonResult<Void> approveContract(
            @PathVariable Long contractId) {
        
        Long currentUserId = getCurrentUserId();
        return contract7Service.approveContract(contractId, currentUserId);
    }

    /**
     * 审核驳回合同
     */
    @PostMapping("/{contractId}/reject")
    public JsonResult<Void> rejectContract(@PathVariable Long contractId) {
        
        Long currentUserId = getCurrentUserId();
        return contract7Service.rejectContract(contractId, currentUserId);
    }

    /**
     * 合同完成
     */
    @PostMapping("/{contractId}/complete")
    public JsonResult<Void> completeContract(@PathVariable Long contractId) {
        Long currentUserId = getCurrentUserId();
        return contract7Service.completeContract(contractId, currentUserId);
    }

    /**
     * 获取合同状态枚举
     */
    @GetMapping("/status-list")
    public JsonResult<ContractStatus[]> getStatusList() {
        return new JsonResult().success(ContractStatus.values());
    }

    /**
     * 获取合同商品明细
     */
    @GetMapping("/{contractId}/items")
    public JsonResult<List<Contract7DetailItemDTO>> getContractItems(@PathVariable Long contractId) {
        List<Contract7DetailItemDTO> items = contract7Service.getContractItems(contractId);
        return new  JsonResult().success(items);
    }

    /**
     * 获取当前用户ID
     */
    private Long getCurrentUserId() {

        //return  UserContextUtil.getUser().getId();
      return 1L;
    }

}

